//
//  MainController.m
//  MixDrinks
//
//  Created by admin on 2016/12/27.
//  Copyright © 2016年 Zhongshan Camry Electronic Company Limited. All rights reserved.
//
#define NavShowAnimDuration 0.25
#define keyPath(objc,keyPath) @(((void)objc.keyPath,#keyPath))
#define trans 200 * Width_Scale
#import "MainController.h"
#import "NavigationController.h"
#import "LeftButton.h"
#import "titleButton.h"
#import "CocktailController.h"

@interface MainController ()<leftButtonDelegate>
@property (nonatomic, weak) NavigationController *navigation;
@property (nonatomic, assign) CGFloat translation;
@property (nonatomic, strong) UIPanGestureRecognizer *pan;
@property (nonatomic,assign) CGFloat target;
@property (nonatomic,strong) UIButton *cover;
@end

@implementation MainController

#pragma mark - 懒加载
-(UIButton *)cover {
    if (_cover == nil) {
        _cover = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cover addTarget:self action:@selector(coverClick:) forControlEvents:UIControlEventTouchUpInside];
        _cover.frame = self.navigation.view.bounds;
    }
    return _cover;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupSubviews];
}

- (void)setupSubviews {
    CocktailController *drinks = [[CocktailController alloc] init];
    [self setupVC:drinks title:@"酒单库"];
    UIViewController *menu = [[UIViewController alloc] init];
    [self setupVC:menu title:@"自定义酒单"];
    UIViewController *weight = [[UIViewController alloc] init];
    [self setupVC:weight title:@"称重"];
    UIViewController *unit = [[UIViewController alloc] init];
    [self setupVC:unit title:@"单位设置"];
    UIViewController *feedBack = [[UIViewController alloc] init];
    [self setupVC:feedBack title:@"意见反馈"];
    UIViewController *app = [[UIViewController alloc] init];
    [self setupVC:app title:@"版本信息"];
    UIViewController *about = [[UIViewController alloc] init];
    [self setupVC:about title:@"关于我们"];
    LeftButton *leftMenu = [[LeftButton alloc] init];
    leftMenu.delegate = self;
    leftMenu.width = 200 * Width_Scale;
    leftMenu.y = 60 * Width_Scale;
    leftMenu.height = SCREEN_HEIGHT - leftMenu.y;
    [self.view insertSubview:leftMenu atIndex:0];
    //添加拖拽手势
    UIPanGestureRecognizer *rightPan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(rightPan:)];
    [self.view addGestureRecognizer:rightPan];
}

- (void)setupVC:(UIViewController *)controller title:(NSString *)title {
    controller.view.backgroundColor = [UIColor whiteColor];
    //标题
    titleButton *titleBtn = [[titleButton alloc] init];
    titleBtn.title = title;
    controller.navigationItem.titleView = titleBtn;
    //左按钮
    controller.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImageName:@"QQ20161227-0" target:self action:@selector(leftBtn)];
    NavigationController *NVC = [[NavigationController alloc]initWithRootViewController:controller];
    [self addChildViewController:NVC];
}

- (void)leftBtn {
    [UIView animateWithDuration:NavShowAnimDuration animations:^{
        self.navigation.view.frame = [self frameWithOffsetX:trans];
        // 添加一个遮盖
        [self.navigation.view addSubview:self.cover];
    }];
}

- (void)coverClick:(UIButton *)cover {
    [UIView animateWithDuration:NavShowAnimDuration animations:^{
        self.navigation.view.frame = self.view.bounds;
    } completion:^(BOOL finished) {
        [cover removeFromSuperview];
    }];
}

#pragma mark - 实现手势
-(void)rightPan:(UIPanGestureRecognizer *)pan {
    CGFloat offsetX = [pan translationInView:self.navigation.view].x;
    self.navigation.view.frame = [self frameWithOffsetX:offsetX];
    [pan setTranslation:CGPointZero inView:self.navigation.view];
    if (pan.state == UIGestureRecognizerStateEnded) {
        _target = 0;
        if (self.navigation.view.x > SCREEN_WIDTH/4) {
            _target = trans;
            [self.navigation.view addSubview:self.cover];
        } else if (CGRectGetMaxX(self.navigation.view.frame) < SCREEN_WIDTH/4) {
            _target = 0;
        }
        CGFloat offsetX = _target - self.navigation.view.frame.origin.x;
        [UIView animateWithDuration:NavShowAnimDuration animations:^{
            self.navigation.view.frame = [self frameWithOffsetX:offsetX];
        } completion:^(BOOL finished) {
            if (self.cover && self.navigation.view.x < SCREEN_WIDTH/4) {
                [self.cover removeFromSuperview];
            }
        }];
    }
}

- (CGRect)frameWithOffsetX:(CGFloat)offsetX {
    CGRect frame = self.navigation.view.frame;
    CGFloat x = frame.origin.x += offsetX;
    if (x <= 0) {
        x = 0;
    }
    if (x > trans) {
        x = trans;
    }
    return CGRectMake(x, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}

- (void)leftButton:(LeftButton *)button selectFromeIndex:(NSInteger)fromIndex ToIndex:(NSInteger)toIndex {
    NavigationController *oldNav = self.childViewControllers[fromIndex];
    [oldNav.view removeFromSuperview];
    NavigationController *newNav = self.childViewControllers[toIndex];
    newNav.view.transform = oldNav.view.transform;
    newNav.view.layer.shadowColor = [UIColor blackColor].CGColor;
    newNav.view.layer.shadowOffset = CGSizeMake(-3, 0);
    newNav.view.layer.shadowOpacity = 0.2;
    [self.view addSubview:newNav.view];
    self.navigation = newNav;
    [UIView animateWithDuration:NavShowAnimDuration animations:^{
        self.navigation.view.frame = self.view.bounds;
    }completion:^(BOOL finished) {
        if (self.cover) {
            [self.cover removeFromSuperview];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
